package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.common.HrLogKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IApplyToProgEmployDAO;
import org.jsola.hr.dto.ApplyToProgEmployAddDTO;
import org.jsola.hr.dto.ApplyToProgEmployUpdateDTO;
import org.jsola.hr.entity.ApplyToProgEmployDO;
import org.jsola.hr.query.ApplyToProgEmployQuery;
import org.jsola.hr.service.IApplyToProgEmployService;
import org.jsola.hr.vo.ApplyToProgEmployListVO;
import org.jsola.hr.vo.ApplyToProgEmployVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrApplyToProgEmployServiceImpl")
public class ApplyToProgEmployServiceImpl implements IApplyToProgEmployService {

    @Autowired
    private IApplyToProgEmployDAO applyToProgEmployDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public ApplyToProgEmployVO save(ApplyToProgEmployAddDTO applyToProgEmployAddDTO, TokenUser tokenUser) {
        // 转DO
        ApplyToProgEmployDO applyToProgEmployDO = applyToProgEmployAddDTO.to(ApplyToProgEmployDO.class);
        // 保存
        applyToProgEmployDO = save(applyToProgEmployDO, tokenUser.getSiteId(), tokenUser.getUserId());
        HrLogKit.logForSave("新增方案适用设置聘用形式", "saveApplyToProgEmploy", applyToProgEmployDO.getId(), tokenUser);
        return applyToProgEmployDO.to(ApplyToProgEmployVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(ApplyToProgEmployUpdateDTO applyToProgEmployUpdateDTO, TokenUser tokenUser) {
        //转DO
        ApplyToProgEmployDO applyToProgEmployDO = applyToProgEmployUpdateDTO.to(ApplyToProgEmployDO.class);
        //根据主键更新，只更新非null值
        HrLogKit.logForUpdate("修改方案适用设置聘用形式", "updateApplyToProgEmploy", applyToProgEmployDO.getId(), tokenUser);
        return updateByIdSelective(applyToProgEmployDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... applyToProgEmployIds) {
        HrLogKit.logForDelete("批量删除方案适用设置聘用形式", "deleteApplyToProgEmployByIds"
                , Arrays.toString(applyToProgEmployIds), tokenUser);
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) applyToProgEmployIds);
    }


    @Override
    public ApplyToProgEmployVO selectById(String applyToProgEmployId, String siteId) {
        ApplyToProgEmployDO applyToProgEmployDO = selectDOById(applyToProgEmployId, siteId);
        if (applyToProgEmployDO == null) {
            return null;
        }
        return applyToProgEmployDO.to(ApplyToProgEmployVO.class);
    }

    @Override
    public List<ApplyToProgEmployListVO> select(ApplyToProgEmployQuery applyToProgEmployQuery, String siteId) {
        List<ApplyToProgEmployDO> applyToProgEmployDOList = selectDO(applyToProgEmployQuery, siteId);
        if(CollectionUtils.isEmpty(applyToProgEmployDOList)) {
            return applyToProgEmployDOList == null ? null : new ArrayList<>();
        }
        return applyToProgEmployDOList.stream()
                .map(applyToProgEmployDO -> applyToProgEmployDO.to(ApplyToProgEmployListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(ApplyToProgEmployQuery applyToProgEmployQuery, String siteId) {
        Example example = buildExample(applyToProgEmployQuery,siteId);
        return applyToProgEmployDAO.selectCountByExample(example);
    }

    @Override
    public Page<ApplyToProgEmployListVO> selectPage(ApplyToProgEmployQuery applyToProgEmployQuery, String siteId) {
        Example example = buildExample(applyToProgEmployQuery,siteId);
        Page<ApplyToProgEmployDO> page = applyToProgEmployDAO.selectPageByExample(example,
                applyToProgEmployQuery.getPageNo(),
                applyToProgEmployQuery.getPageSize());

        return page.to(ApplyToProgEmployListVO.class);
    }

    @Override
    public ApplyToProgEmployDO selectDOById(String applyToProgEmployId, String siteId) {
        return listById(applyToProgEmployId, siteId);
    }

    @Override
    public List<ApplyToProgEmployDO> selectDO(ApplyToProgEmployQuery applyToProgEmployQuery, String siteId) {
        Example example = buildExample(applyToProgEmployQuery,siteId);
        return applyToProgEmployDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param applyToProgEmployQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(ApplyToProgEmployQuery applyToProgEmployQuery, String siteId) {
        Example example = new Example(ApplyToProgEmployDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        if (applyToProgEmployQuery.getProgId() != null) {
            example.and().andLike("progId", "%" + applyToProgEmployQuery.getProgId() + "%");
        }
        if (applyToProgEmployQuery.getEmployId() != null) {
            example.and().andLike("employId", "%" + applyToProgEmployQuery.getEmployId() + "%");
        }
        if (applyToProgEmployQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", applyToProgEmployQuery.getCompanyId());
        }
        if (applyToProgEmployQuery.getGmtCreateStart() != null &&
                applyToProgEmployQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", applyToProgEmployQuery.getGmtCreateStart(),
                    applyToProgEmployQuery.getGmtCreateEnd());
        }
        if (applyToProgEmployQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + applyToProgEmployQuery.getCreateUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,applyToProgEmployQuery.getOrders());
        return example;
    }
}




